﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace XbmcImdbScoreUpdater
{
    static class ImdbConnector
    {
        public static ImdbData GetImdbData(string ImdbID)
        {
            List<ImdbData> ratings = new List<ImdbData>();
            //string id = "tt0169547";
            var xml = XDocument.Load(@"http://imdbapi.org/?id=" + ImdbID + "&type=xml&plot=simple&episode=1&lang=en-US&aka=simple&release=simple&business=0&tech=0");

            var data = from item in xml.Descendants("imdbdocument")
                       select new
                       {
                           title = item.Element("title").Value,
                           rating = item.Element("rating").Value,
                           count = item.Element("rating_count").Value
                       };

            foreach (var p in data)
            {
                ratings.Add(new ImdbData(ImdbID, p.title, Convert.ToDecimal(p.rating), Convert.ToInt32(p.count)));
            }

            return ratings[0];
        }
    }
}
